package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.event.RxBus;
import com.cn.kabalifeuser.event.RxBusBean;
import com.cn.kabalifeuser.ui.my.adapter.PagerAdapter;
import com.cn.kabalifeuser.ui.my.bean.CouponTypeListBean;
import com.cn.kabalifeuser.ui.my.bean.MyCouponListBean;
import com.cn.kabalifeuser.ui.my.fragment.CardrollFragments;
import com.cn.kabalifeuser.ui.my.fragment.CardrollPresenter;
import com.cn.kabalifeuser.ui.my.fragment.CardrollView;
import com.cn.kabalifeuser.utils.RxToast;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/7.
 * function: 卡卷
 */
public class CardrollActivity extends IBaseActivity<CardrollView, CardrollPresenter> implements CardrollView, OnTabSelectListener {
    @BindView(R.id.tv_btn_1)
    TextView tvBtn1;
    @BindView(R.id.tv_btn_2)
    TextView tvBtn2;
    @BindView(R.id.tv_btn_3)
    TextView tvBtn3;
    @BindView(R.id.slitab)
    SlidingTabLayout slitab;
    @BindView(R.id.vp_type)
    ViewPager vpType;
    List<String> mList = new ArrayList<>();
    PagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<>();

    int barID1;
    int barID2;
    int barID3;

    @Override
    protected CardrollPresenter createPresenter() {
        return new CardrollPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cardroll;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("优惠券");
        mPresenter.onCouponTypeData(LoginHelper.getLonginData().getResult());


    }

    private void AddTitleListData() {
        mList.clear();
        mList.add("未使用");
        mList.add("已使用");
        mList.add("已过期");
    }

    private void initType(int index) {
        switch (index) {
            case 1:
                tvBtn1.setTextColor(getResources().getColor(R.color.main_white));
                tvBtn1.setBackgroundColor(getResources().getColor(R.color.main_color));

                tvBtn2.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtn2.setBackgroundColor(getResources().getColor(R.color.text_f8f8f8));

                tvBtn3.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtn3.setBackgroundColor(getResources().getColor(R.color.text_f8f8f8));
                break;
            case 2:
                tvBtn1.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtn1.setBackgroundColor(getResources().getColor(R.color.text_f8f8f8));

                tvBtn2.setTextColor(getResources().getColor(R.color.main_white));
                tvBtn2.setBackgroundColor(getResources().getColor(R.color.main_color));

                tvBtn3.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtn3.setBackgroundColor(getResources().getColor(R.color.text_f8f8f8));
                break;
            case 3:
                tvBtn1.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtn1.setBackgroundColor(getResources().getColor(R.color.text_f8f8f8));

                tvBtn2.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtn2.setBackgroundColor(getResources().getColor(R.color.text_f8f8f8));

                tvBtn3.setTextColor(getResources().getColor(R.color.main_white));
                tvBtn3.setBackgroundColor(getResources().getColor(R.color.main_color));
                break;
        }
    }

    @OnClick({R.id.tv_btn_1, R.id.tv_btn_2, R.id.tv_btn_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_1:
                initType(1);
                RxBus.getDefault().post(new RxBusBean(1, 1, barID1));
                break;
            case R.id.tv_btn_2:
                initType(2);
                RxBus.getDefault().post(new RxBusBean(1, 1, barID2));
                break;
            case R.id.tv_btn_3:
                initType(3);
                RxBus.getDefault().post(new RxBusBean(1, 1, barID3));
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onCouponTypeSuccess(CouponTypeListBean bean) {
        tvBtn1.setText(bean.getResult().get(0).getName());
        tvBtn2.setText(bean.getResult().get(1).getName());
        tvBtn3.setText(bean.getResult().get(2).getName());


        barID1 = bean.getResult().get(0).getId();
        barID2 = bean.getResult().get(1).getId();
        barID3 = bean.getResult().get(2).getId();

        initType(1);
        AddTitleListData();
        mFragments.add(CardrollFragments.getInstance(bean.getResult().get(0).getId(), 1));
        mFragments.add(CardrollFragments.getInstance(bean.getResult().get(1).getId(), 2));
        mFragments.add(CardrollFragments.getInstance(bean.getResult().get(2).getId(), 3));
        mAdapter = new PagerAdapter(getSupportFragmentManager(), mList, mFragments);
        vpType.setAdapter(mAdapter);
        vpType.setOffscreenPageLimit((mList.size() - 1));
        slitab.setViewPager(vpType);
        slitab.setOnTabSelectListener(this);
//        slitab.setCurrentTab(sign);
    }

    @Override
    public void onCouponListSuccess(MyCouponListBean bean) {

    }
}
